import pandas as pd
import numpy as np
import os
import json
import networkx as nx
import pickle

from config import *

def read_data(path, rebuild=False):
    event_path = os.path.join(path, 'event_list.json')
    if os.path.exists(event_path) and rebuild==False:
        with open (event_path) as f:
            event_list = json.load(f)       
    print("load event info complete")
    
    member_path = os.path.join(path, 'member_list.json')     
    if os.path.exists(member_path) and rebuild==False:
        with open(member_path) as f:
            member_list = json.load(f)
    print("load member info complete")

    group_path = os.path.join(path, 'group_list.json')     
    if os.path.exists(group_path) and rebuild==False:
        with open(group_path) as f:
            group_list = json.load(f)
    print("load group info complete")

    topic_json_path = os.path.join(path, "topic_dict.json")
    topic_dict = dict()
    if os.path.exists(topic_json_path) and (not rebuild):
        with open(topic_json_path) as f:
            topic_dict = json.load(f)
    print("load topic info complete")
    
    return event_list, topic_dict, member_list, group_list


def load_all_graphs(path):
    """
    读取所有的网络
    """
    with open(path, 'rb') as file:
        all_graphs = pickle.load(file)
    
    return all_graphs

def predict_using_distance(event_id, member_id, all_graphs, member_list, group_list):
    event_id = str(event_id)
    member_id = str(member_id)
    
    cur_event = 0
    event_city = 0
    event_country = 0
    member_city = 0
    member_country = 0
      
    for event in all_graphs:
        if(event.id==event_id):
            cur_event = event
            break
    if(cur_event==0):return -1
    cur_event_group_id = cur_event.group_id
    
    for group in group_list:        
        if(group['id'] == cur_event_group_id):
            event_city = group['city']
            event_country = group['country']
            break
    
    for member in member_list:
        if(member['id']==member_id):
            member_city = member['city']
            member_country = member['country']
            break
            
    
    if(event_city == 0 or event_country==0 or member_city==0 or member_country==0):
        # get into trouble
        return -1
    if(member_city == event_city):
        # in the same sity, very possible
        return 2
    elif(member_country == event_country):
        # in the same country, less possible
        return 1
    # not in the same country, pretty impossible
    else: return 0
        
def test():
    event_list, topic_dict, member_list, group_list = read_data(json_path)
    all_graphs = load_all_graphs(pkl_path)
    i = 0
    
    # 统计指标
    same_country_diff_city_yes = 0 
    same_country_diff_city_no = 0
    diff_country_and_yes = 0
    diff_country_and_no = 0
    same_city_and_yes = 0
    same_city_and_no = 0
    
    
    for event in event_list:
        print(i)
        i+=1
        # if(i>100):break
        for member_id, true_response in event["member and response"].items():
            predict_response = predict_using_distance(event['id'], member_id, all_graphs, member_list, group_list)   
            # true_response may be waitlist, can be seen as no
            print(true_response, predict_response)
            # 0 0 286247 246088 138048 78237
            if((true_response=='no' or true_response=='waitlist') and predict_response==0):diff_country_and_no+=1
            if(true_response=='yes' and predict_response==0):diff_country_and_yes+=1
            if((true_response=='no' or true_response=='waitlist') and predict_response==1):same_country_diff_city_no+=1
            if(true_response=='yes' and predict_response==1):same_country_diff_city_yes+=1
            if((true_response=='no' or true_response=='waitlist') and predict_response==2):same_city_and_no+=1
            if(true_response=='yes' and predict_response==2):same_city_and_yes+=1
    print(same_country_diff_city_yes,same_country_diff_city_no,diff_country_and_yes,diff_country_and_no,same_city_and_yes,same_city_and_no)
                 
if __name__ == "__main__":    
    test()